#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/timerfd.h>
#include<time.h>
#include<iostream>

int main()
{
    int timefd=timerfd_create(CLOCK_MONOTONIC,0);

    itimerspec timeout;
    timeout.it_value.tv_sec=1;//第一次超时时间，单位为秒
    timeout.it_interval.tv_sec=5;//第一次超时后的超时时间
    timerfd_settime(timefd,0,&timeout,nullptr);

    time_t start=time(NULL);
    while(1)
    {
        u_int32_t message;
        read(timefd,&message,8);
        printf("距离上次超时%ld次",message);//读取一次就清0
        time_t now = time(NULL);  
        std::cout<<"间隔"<<difftime(now, start) << " seconds" << std::endl;  
    }

    return 0;
}